﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib.DoubleClickHandler
{
    public interface ICustomCommandMapper
    {
        string GetMappedCustomCommand(ObjectId entId);
    }

    internal class LineCustomCommandMapper : ICustomCommandMapper
    {
        private Type _entityType;
        private RXClass _rxClass;

        public LineCustomCommandMapper()
        {
            _entityType = typeof(Line);
            _rxClass = RXClass.GetClass(_entityType);
        }

        public Type EntityType => _entityType;

        public string GetMappedCustomCommand(ObjectId entId)
        {
            if (entId.ObjectClass != _rxClass)
                return null;

            // 在这里做一些根据实体附加信息的判断，看是否需要触发自定义双击功能
            // 例如：检查实体的扩展字典或其他属性

            // 我们在这里判断line是否是从左向右创建的。
            Point3d startPoint, endPoint;
            Database db = entId.Database;
            using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
            {
                var line = UtilFunc.GetObject(trans, entId, OpenMode.ForRead) as Line;
                if (line == null) return null;

                startPoint = line.StartPoint;
                endPoint = line.EndPoint;
                trans.Commit();
            }

            if (startPoint.X < endPoint.X)
            {
                // 如果是从左向右创建的线段，返回自定义命令名称
                return "MyLineEditorCommand1";
            }
            else
            {
                // 否则返回反向命令
                return "MyLineEditorCommand2";
            }
        }
    }

    internal class TextCustomCommandMapper : ICustomCommandMapper
    {
        private Type _entityType;
        private RXClass _rxClass;
        
        public Type EntityType => _entityType;

        public TextCustomCommandMapper()
        {
            _entityType = typeof(DBText);
            _rxClass = RXClass.GetClass(_entityType);
        }

        public string GetMappedCustomCommand(ObjectId entId)
        {
            if (entId.ObjectClass != _rxClass) return null;

            // 在这里做一些根据实体附加信息的判断，看是否需要触发自定义双击功能
            
            return "MyTextEditorCommand"; // 返回自定义命令名称
        }
    }

    internal class BlockCustomCommandMapper : ICustomCommandMapper
    {
        private Type _entityType;
        private RXClass _rxClass;
        private Dictionary<string, string> _dictCommands;

        public BlockCustomCommandMapper(Dictionary<string, string> blkEditCommands)
        {
            _entityType = typeof(BlockReference);
            _rxClass = RXClass.GetClass(_entityType);
            _dictCommands = blkEditCommands;
        }

        public Type EntityType => _entityType;

        public string GetMappedCustomCommand(ObjectId entId)
        {
            if (entId.ObjectClass != _rxClass) return null;


            // 在这里做一些根据实体附加信息的判断，看是否需要触发自定义双击功能
            // 例如：检查实体的扩展字典或其他属性
            // 或者判断一些块定义的属性之类的

            string bName = GetBlockName(entId);
            if (_dictCommands.ContainsKey(bName)) return _dictCommands[bName];

            return null;
        }

        private static string GetBlockName(ObjectId entId)
        {
            string blkName = "";

            using (var trans = entId.Database.TransactionManager.StartOpenCloseTransaction())
            {
                BlockReference bf = UtilFunc.GetObject(trans, entId, OpenMode.ForRead) as BlockReference;
                if (bf != null && bf.IsDynamicBlock)
                {
                    if (bf.Name.StartsWith("*"))
                    {
                        BlockTableRecord br = UtilFunc.GetObject(trans, bf.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                        blkName = br?.Name ?? "";
                    }
                    else
                        blkName = bf.Name;
                }
                else
                    blkName = bf.Name;

                trans.Commit();
            }

            return blkName;
        }
    }

    public class CustomComandsFactory
    {
        public static CustomCommandMappers CreateDefaultCustomCommandMappers()
        {
            var mappers = new CustomCommandMappers();

            // 添加线段的自定义命令映射器
            mappers.Add(typeof(Line), new LineCustomCommandMapper());
            mappers.Add(typeof(DBText), new TextCustomCommandMapper());

            // 添加块的自定义命令映射器
            var blockCommands = new Dictionary<string, string>
            {
                { "testblock1", "MyBlockEditorCommand1" },
                { "testblock2", "MyBlockEditorCommand2" }
            };
            mappers.Add(typeof(BlockReference), new BlockCustomCommandMapper(blockCommands));
            return mappers;
        }
    }
}
